package com.lst.projectlib.component.guidegallery;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lst.projectlib.entity.News;
import com.lst.projectlib.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

public class GuideGalleryIndicator extends RelativeLayout {
	TextView tvTitle;
	LinearLayout layPoint;
	private int pagePositon;// 当前页面位置
	private int defaultPic = android.R.drawable.presence_invisible;// 默认图片id
	private int activePic = android.R.drawable.presence_online;// 获得页面图片id
	private List<News> list = new ArrayList<News>();

	public int getDefaultPic() {
		return defaultPic;
	}

	public void setDefaultPic(int defaultPic) {
		this.defaultPic = defaultPic;
	}

	public int getActivePic() {
		return activePic;
	}

	public void setActivePic(int activePic) {
		this.activePic = activePic;
	}

	public int getPagePositon() {
		return pagePositon;
	}

	public void setPagePositon(int pagePositon) {
		this.pagePositon = pagePositon;
	}

	public int getPageCount() {
		return list.size();
	}

	public void setData(List<News> list) {
		this.list = list;

		if (layPoint != null) {
			layPoint.removeAllViews();
		}
		addView();
	}

	public GuideGalleryIndicator(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	public GuideGalleryIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	public GuideGalleryIndicator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		tvTitle = new TextView(getContext());
		tvTitle.setTextColor(getResources().getColor(android.R.color.white));
		tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
		tvTitle.setMinLines(2);
		tvTitle.setMaxLines(2);
		LayoutParams tvTitleLP = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		addView(tvTitle,tvTitleLP);

		layPoint = new LinearLayout(getContext());
		layPoint.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
		layPoint.setOrientation(LinearLayout.HORIZONTAL);
		int topBottomPadding = DensityUtil.dip2px(getContext(),2);
		layPoint.setPadding(0,0,0,topBottomPadding);
		LayoutParams layPointLP = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		addView(layPoint, layPointLP);
	}

	private void addView() {
		// TODO Auto-generated method stub
		for (int i = 0; i < getPageCount(); i++) {
			ImageView pointView = new ImageView(getContext());
			if (i == 0) {
				pointView.setBackgroundResource(getActivePic());
				setPagePositon(i);
			} else{
				pointView.setBackgroundResource(getDefaultPic());
			}

			// Set the size and margin of the dot.
			int widthHeight = DensityUtil.dip2px(getContext(),7);
			LinearLayout.LayoutParams imageViewLP = new LinearLayout.LayoutParams(widthHeight, widthHeight);
			int leftRightMargin = DensityUtil.dip2px(getContext(),5);
			imageViewLP.setMargins(leftRightMargin, 0, 0, 0);
			layPoint.addView(pointView, imageViewLP);
		}
	}

	public void changePointView(int cur) {
		View view = layPoint.getChildAt(getPagePositon());
		View curView = layPoint.getChildAt(cur);
		if (view != null && curView != null) {
			ImageView pointView = (ImageView) view;
			ImageView curPointView = (ImageView) curView;
			pointView.setBackgroundResource(getDefaultPic());
			curPointView.setBackgroundResource(getActivePic());

			tvTitle.setText(list.get(cur).getNewsTitle());
			setPagePositon(cur);
		}
	}
}
